package com.xinqi.modules.course.image.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.course.image.dto.req.CourseImageCreateDTO;
import com.xinqi.modules.course.image.dto.req.CourseImageDeleteDTO;
import com.xinqi.modules.course.image.dto.req.CourseImageQueryDTO;
import com.xinqi.modules.course.image.dto.req.CourseImageUpdateDTO;
import com.xinqi.modules.course.image.dto.rsp.CourseImageResultDTO;
import com.xinqi.modules.course.image.convert.CourseImageConvert;
import com.xinqi.modules.course.image.domain.CourseImageEntity;
import com.xinqi.modules.course.image.manager.CourseImageManager;
import com.xinqi.modules.course.image.service.CourseImageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 课程背景封面图表服务层实现
 *
 * @author: system
 * @date: 2023/03/22
 */
@Service("courseImageService")
@RequiredArgsConstructor
public class CourseImageServiceImpl implements CourseImageService {
    private final CourseImageConvert courseImageConvert;
    private final CourseImageManager courseImageManager;

    /**
     * 分页查询 课程背景封面图表数据
     */
    @Override
    @DS("slaver")
    public PageInfo<CourseImageResultDTO> page(Param pageable, CourseImageQueryDTO query) {
        IPage<CourseImageEntity> page = courseImageManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, courseImageConvert::convert);
    }

    /**
     * 查询 课程背景封面图表数据
     */
    @Override
    public List<CourseImageResultDTO> find(CourseImageQueryDTO query) {
        return courseImageManager.list(wrapper(query)).stream()
                .map(courseImageConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<CourseImageEntity> wrapper(CourseImageQueryDTO query) {
        LambdaQueryWrapper<CourseImageEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(CourseImageEntity::getId, v));
        Queries.accept(query.getDictType(), v -> wrapper.eq(CourseImageEntity::getDictType, v));
        return wrapper;
    }

    /**
     * 根据id查询 课程背景封面图表数据
     */
    @Override
    public CourseImageResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return Optional.ofNullable(courseImageManager.findById(id)).map(courseImageConvert::convert).orElse(null);
    }

    /**
     * 新增 课程背景封面图表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(CourseImageCreateDTO dto) {
        CourseImageEntity entity = courseImageConvert.create(dto);
        courseImageManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 课程背景封面图表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(CourseImageUpdateDTO dto) {
        CourseImageEntity old = Optional.ofNullable(courseImageManager.getById(dto.getId()))
                .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException("数据不存在"));
        CourseImageEntity entity = courseImageConvert.update(dto);
        return courseImageManager.updateById(entity);
    }

    /**
     * 根据id删除 课程背景封面图表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return courseImageManager.removeById(id);
    }

    /**
     * 根据id删除 课程背景封面图表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(CourseImageDeleteDTO dto) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(dto.getId()), "ID不能为空");
        return courseImageManager.deleteById(dto.getId());
    }

}
